package be.ipl.finito.domaine;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;

@SuppressWarnings("serial")
@Entity
@Table(name = "JOUEURS", schema = "FINITO")
public class Joueur implements Serializable {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id_joueur;

	@NotNull
	@Column(unique = true)
	private String login;

	@NotNull
	private String password;

	@Column(nullable = true)
	private String nom;

	@Column(nullable = true)
	private String prenom;

	@Column(nullable = true)
	private String email;

	@Transient
	private List<Plateau> plateaux;

	protected Joueur() {
	}

	private Joueur(Builder b) {
		this.login = b.login;
		this.nom = b.nom;
		this.email = b.email;
		this.password = b.password;
		this.prenom = b.prenom;
		plateaux = new ArrayList<Plateau>();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id_joueur;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Joueur other = (Joueur) obj;
		if (id_joueur != other.id_joueur)
			return false;
		return true;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getPrenom() {
		return prenom;
	}

	public void setPrenom(String prenom) {
		this.prenom = prenom;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public int getIdJoueur() {
		return id_joueur;
	}

	public List<Plateau> getPlateaux() {
		return plateaux;
	}

	public void setPlateaux(List<Plateau> plateaux) {
		this.plateaux = plateaux;
	}

	public void ajouterPlateau(Plateau plateau) {
		this.plateaux.add(plateau);
	}

	public void enleverPlateau(Plateau plateau) {
		this.plateaux.remove(plateau);
	}

	public static class Builder {

		private String login;
		private String password;
		private String nom;
		private String prenom;
		private String email;

		public Builder(String login, String password) {
			this.login = login;
			this.password = password;
		}

		public Builder nom(String nom) {
			this.nom = nom;
			return this;
		}

		public Builder prenom(String prenom) {
			this.prenom = prenom;
			return this;
		}

		public Builder email(String email) {
			this.email = email;
			return this;
		}

		public Joueur build() {
			return new Joueur(this);
		}

	}

}